﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Diagnostics;
using HaiHui.Data;

namespace HaiHui.Business
{
    public class Picture
    {
        private object objLocker = new object();

        /// <summary>
        /// Albumul din care face parte
        /// </summary>
        public Album Album { get; set; }
        public int Id { get; set; }
        public string Name { get; set; }
        public string Descriere { get; set; }
        public string CuvinteCheie { get; set; }
        public bool AcceptaComentarii { get; set; }
        public bool Public { get; set; }

        public string SmallImageUrl { get; set; }
        public string LargeImageUrl { get; set; }

        private BitmapFrame m_SmallImage;
        public BitmapFrame SmallImage
        {
            get
            {
                //suntem smecheri, folosim lock... 
                lock (objLocker)
                {
                    return m_SmallImage;
                }
            }
        }

        private BitmapFrame m_LargeImage;
        public BitmapFrame LargeImage
        {
            get
            {
                lock (objLocker)
                {
                    return m_LargeImage;
                }
            }
        }

        /// <summary>
        /// Incarca cele 2 magnifici imagini (a' mica + a' mare) pt fiecare Picture :) ce frumos suna
        /// </summary>
        public void GetImages()
        {

            Trace.WriteLine(
                String.Format("Incarcam poza {0}, thread Id {1}", Name, Thread.CurrentThread.ManagedThreadId)
                );

            JpegBitmapDecoder decoderSmallImage = new JpegBitmapDecoder(
                new Uri(this.SmallImageUrl, UriKind.RelativeOrAbsolute), BitmapCreateOptions.None, BitmapCacheOption.Default);

            BitmapFrame smallImageFrame = decoderSmallImage.Frames[0];
            m_SmallImage = (BitmapFrame)smallImageFrame.GetAsFrozen();

            JpegBitmapDecoder decoderLargeImage = new JpegBitmapDecoder(
                new Uri(this.LargeImageUrl), BitmapCreateOptions.None, BitmapCacheOption.Default);

            BitmapFrame largeImageFrame = decoderLargeImage.Frames[0];
            m_LargeImage = (BitmapFrame)largeImageFrame.GetAsFrozen();
        }

        /// <summary>
        /// cum ii zice, aia face.
        /// </summary>
        public void GetDetailsData()
        {
            string detailsUrl = String.Concat(Constants.PictureDetailsBaseURL, Id.ToString());

            string detailsHtml = HttpUtils.Get(detailsUrl);

            int index, length;
            string startPattern, endPattern;

            do
            {
                startPattern = "name=\"nume\" value=\"";
                endPattern = "\" /><br />";
                index = detailsHtml.IndexOf(startPattern);

                if (index > -1)
                {
                    detailsHtml = detailsHtml.Substring(index+startPattern.Length);
                    length = detailsHtml.IndexOf(endPattern);
                    this.Name = detailsHtml.Substring(0, length);

                    DataStore.Worker.ReportProgress(1, String.Format("Incarcam poza \"{0}\"", this.Name));

                    this.Descriere = Utils.ScrapeNext(ref detailsHtml, "<textarea name=\"descriere\">", "</textarea>");
                    this.CuvinteCheie = Utils.ScrapeNext(ref detailsHtml, "name=\"tags\" value=\"", "\" />");

                    if (detailsHtml.Contains("name=\"accept_comentarii\" value=\"1\" checked='checked'"))
                        AcceptaComentarii = true;
                    if (detailsHtml.Contains("name=\"activ\" value=\"1\" checked='checked'"))
                        Public = true;

                }

            } while (index != -1);
        }

    }
}
